﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OctoOS;


/// <summary>
/// Summary description for Helper
/// </summary>
public static class Helper
{
    public static class Categories
    {
        public static bool UpdateCategoryCache()
        {
            int categoryVersionCache = -1;
            int categoryVersionDB;

            if (HttpContext.Current.Cache[OOSRequest.Current.ResourceID + "CategoryVersion"] != null)
            {
                categoryVersionCache = (int)HttpContext.Current.Cache[OOSRequest.Current.ResourceID + "CategoryVersion"];
            }
            else
            {
                HttpContext.Current.Cache[OOSRequest.Current.ResourceID + "CategoryVersion"] = -1;
                HttpContext.Current.Cache[OOSRequest.Current.ResourceID + "Categories"] = new Category[] { };
            }

            Category[] categories = DataSource.ClassifiedCategories_GetByVersion(
                OOSRequest.Current.ResourceID,
                categoryVersionCache,
                out categoryVersionDB);

            if (categoryVersionCache != categoryVersionDB)
            {
                HttpContext.Current.Cache[OOSRequest.Current.ResourceID + "Categories"] =
                    categories;
                HttpContext.Current.Cache[OOSRequest.Current.ResourceID + "CategoryVersion"] = categoryVersionDB;
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Category[] GetAllCategories()
        {
            return ((Category[])HttpContext.Current.Cache[OOSRequest.Current.ResourceID + "Categories"]);
        }



        public static Category GetCategory(long categoryID)
        {
            Category[] allCat = Helper.Categories.GetAllCategories();
            return allCat.FirstOrDefault(f => f.ID == categoryID);
        }

        public static Category GetParentCategory(long categoryID)
        {
            Category[] allCat = Helper.Categories.GetAllCategories();
            Category current = allCat.FirstOrDefault(f => f.ID == categoryID);
            if (current != null)
            {
                return allCat.FirstOrDefault(f => f.ID == current.ParentCategoryID);
            }
            return null;
        }

        public static Category[] GetChildCategories(long categoryID)
        {
            Category[] allCat = Helper.Categories.GetAllCategories();
            Category current = allCat.FirstOrDefault(f => f.ID == categoryID);
            if (current != null)
            {
                var found = allCat.Where(f => f.ParentCategoryID == current.ID);
                if (found != null)
                    return found.ToArray();
                else 
                    return null;
            }
            return null;
        }

        public static Category GetGrandParentCategory(long categoryID)
        {
            Category[] allCat = Helper.Categories.GetAllCategories();
            Category current = allCat.FirstOrDefault(f => f.ID == categoryID);
            if (current != null)
            {
                Category parent = allCat.FirstOrDefault(f => f.ID == current.ParentCategoryID);
                if (parent != null)
                {
                    return allCat.FirstOrDefault(f => f.ID == parent.ParentCategoryID);
                }
            }
            return null;
        }

        public static Category[] GetPossibleParentCategories(long categoryID)
        {
            Category[] allCat = Helper.Categories.GetAllCategories();
            List<Category> parentCategories = new List<Category>();
            foreach (var category in allCat.OrderBy(f=>f.ParentCategoryID))
            {
                if (category.ID != categoryID)
                {
                    if (category.ParentCategoryID == 0)
                    {
                        parentCategories.Add(category);
                    }
                    else
                    {
                        var parent = parentCategories.Find(f => f.ID == category.ParentCategoryID && f.ParentCategoryID == 0);
                        if (parent != null)
                        {
                            category.SetLevelName(parent.Name + " - " + category.Name);
                            parentCategories.Insert(parentCategories.IndexOf(parent), category);
                        }
                    }
                }
            }
            return parentCategories.ToArray();
        }
    }
    public enum Currency:byte
    {
        RUB = 1,
        USD,
        EUR,
        GBP,
        AUD
    }

    public enum Entity
    {
        Category,
        Attribute,
        AttributeValue
    }

    public enum PriceType
    {
        Free,
        Contact,
        Money
    }

    public enum AdType:byte
    {
        Offer,
        Need,
        All
    }

    public enum ActionType : byte
    {
        Delete,
        Renew,
        Approve,
        Decline
    }

    public enum AdStatus
    {
        Active,
        Pending,
        Declined,
        Expired
    }

    public enum Comments
    {
        Anyone,
        AuthenticateOnly,
        Disabled
    }
}

public class SelectedAttribute
{
    public long AttributeID;
    public byte AttributeValue;
}